package com.gpy.hospital.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.BookMapper;
import com.gpy.hospital.cms.dao.BookVoMapper;
import com.gpy.hospital.cms.dao.DoctorMapper;
import com.gpy.hospital.cms.pojo.Book;
import com.gpy.hospital.cms.pojo.Doctor;
import com.gpy.hospital.cms.pojo.User;
import com.gpy.hospital.cms.service.IBookService;
import com.gpy.hospital.cms.vo.BookVo;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/27 14:47
 * @Desc
 **/
@Service
public class BookServiceImpl implements IBookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private BookVoMapper bookVoMapper;

    @Override
    public String book(User user, Integer id) {
        Book book = new Book();
        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        book.setDoctorId(doctor.getId());
        book.setDoctorName(doctor.getDoctorName());
        book.setEmail(user.getEmail());
        book.setUserId(user.getId());
        book.setStatus(1);
        int result = bookMapper.insert(book);
        if (result > 0) {
            return "/reception/success";
        }
        return "/reception/fail";
    }

    @Override
    public List<BookVo> getByEmail(String email) {
        return bookVoMapper.getByEmail(email);
    }

    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Book> getByDoctorId(Integer id, String email, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = bookMapper.getByDoctorIdAndEmail(id, email);
        return new PageInfo<>(books);
    }

    @Override
    public ServerResponse deleteId(Integer id) {
        int result = bookMapper.deleteByPrimaryKey(id);
        if (result > 0) {
            return ServerResponse.createByErrorMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }
}
